package cn.wolfcode.trip.base.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public abstract class DateUtil {

    public static String formatDate(Date now) {
        if (now == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(now);
    }

    public static String formatDate2(Date now) {
        if (now == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(now);
    }

    public static Date getEndDate(Date now) {
        if (now == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        c.set(Calendar.HOUR, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND,999);
        Date time = c.getTime();
        return time;
    }

    public static Date getSevenDaysAgo(Date now) {
        if (now == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        c.add(Calendar.DAY_OF_MONTH, -6);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND,0);
        return c.getTime();
    }

    public static Date getAMonthAgo(Date now) {
        if (now == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        c.add(Calendar.MONDAY, -1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND,0);
        return c.getTime();
    }

    //获取一个月的开始时间
    public static Date getAMonthBenin(Date now) {
        if (now == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.set(cal.get(Calendar.YEAR),
                cal.get(Calendar.MONDAY),
                cal.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);

        cal.set(Calendar.MILLISECOND,0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    //日期加减运算
    public static Date getAddDay(Date now,Integer day) {
        if (now == null||day==null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        c.add(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND,0);
        return c.getTime();
    }

    //判断日期是否是当前月
    public static boolean isCurrentMonth(Date date) {
        if (date == null) {
            return false;
        }
        Date now = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        int currentYear = c.get(Calendar.YEAR);
        int currentMonth =c.get(Calendar.MONTH);

        c.setTime(date);
        int year = c.get(Calendar.YEAR);
        int month =c.get(Calendar.MONTH);
        if (currentYear==year && currentMonth==month){
            return true;
        }
        return false;
    }
}
